class ArticlesController < ApplicationController

  #
  # non-RESTful actions defined here
  #

  def train
    Article.train
    render :nothing => true
  end

  def classify
    @article = Article.find(params[:id])
    @article.classify!
    respond_to do |format|
      format.html { logger.debug "html" ; render :nothing => true }
      format.json { logger.debug "json" ; render :json => @article.to_json(Article.show_fxml_params) }
      format.xml  { logger.debug "xml" ; render :xml => @article }
      format.fxml  { logger.debug "fxml" ; render :fxml => @article.to_fxml(Article.show_fxml_params) }
    end
  end

  #
  # end of non-RESTful actions
  #

  # GET /articles
  # GET /articles.xml
  # GET /articles.fxml
  def index
    @articles = Article.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.json {  render :json => @articles }
      format.xml  { render :xml => @articles }
      format.fxml {  render :fxml => @articles.to_fxml(Article.index_fxml_params) }
    end      
  end

  # GET /articles/1
  # GET /articles/1.xml
  # GET /articles/1.fxml
  def show
    @article = Article.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render :json => @article.to_json(Article.show_fxml_params) }
      format.xml  { render :xml => @article }
      format.fxml  { render :fxml => @article.to_fxml(Article.show_fxml_params) }
    end
  end

  # GET /articles/new
  # GET /articles/new.xml
  def new
    @article = Article.new

    respond_to do |format|
      format.html # new.html.erb
      format.json {  render :json => @article }
      format.xml  { render :xml => @article }
    end
  end

  # GET /articles/1/edit
  def edit
    @article = Article.find(params[:id])
  end

  # POST /articles
  # POST /articles.xml
  # POST /articles.fxml
  def create
    @article = Article.new(params[:article])

    respond_to do |format|
      if @article.save
        flash[:notice] = 'Article was successfully created.'
        @article.send_later(:process_new_pdf)
        format.html { redirect_to(@article) }
        format.xml  { render :xml => @article, :status => :created, :location => @article }
        format.json {  render :json => @article.to_json(:methods => [:attachment_url]) }
        format.fxml  { render :fxml => @article.to_fxml(:methods => [:attachment_url]) }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @article.errors, :status => :unprocessable_entity }
        format.json {  render :json => @article.errors }
        format.fxml  { render :fxml => @article.errors }
      end
    end
  end

  # PUT /articles/1
  # PUT /articles/1.xml
  # PUT /articles/1.fxml
  def update
    @article = Article.find(params[:id])
    @saved = @article.update_attributes(params[:article])
    respond_to do |format|
      if @saved
        flash[:notice] = 'Article was successfully updated.'
        format.html { redirect_to(@article) }
        format.xml  { head :ok }
        format.json {  render :json => @article.to_json(:methods => [:attachment_url]) }
        format.fxml  { render :fxml => @article.to_fxml(:methods => [:attachment_url]) }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @article.errors, :status => :unprocessable_entity }
        format.json {  render :json => @article.errors }
        format.fxml  { render :fxml => @article.errors }
      end
    end
  end

  # DELETE /articles/1
  # DELETE /articles/1.xml
  # DELETE /articles/1.fxml
  def destroy
    @article = Article.find(params[:id])
    @article.destroy

    respond_to do |format|
      format.html { redirect_to(articles_url) }
      format.xml  { head :ok }
      format.json  { render :json => @article }
      format.fxml  { render :fxml => @article }
    end
  end
end
